package com.med.controller;

import com.med.dto.RespondDTO;
import com.med.entity.UserDO;
import com.med.service.IUserFrontService;
import com.med.util.ValidateCodeUtil;
import com.med.vo.UserVO;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.controller.BaseController;
import io.jsonwebtoken.Claims;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: internet-based-healthcare
 * @BelongsPackage: com.med.controller
 * @Author: 小吴
 * @CreateTime: 2025-08-17 16:59
 * @Description: TODO
 * @Version: 1.0
 */
@RequestMapping("/userFront")
@RestController
@RefreshScope
public class UserFrontController extends BaseController {

    @Autowired
    private IUserFrontService userFrontService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * @Description: 用户端登录
     * @Param:
     * @return:
     */
    @RequestMapping("/login")
    public RespondDTO login(UserVO userVO) {
        RespondDTO login = userFrontService.login(userVO);
        return login;
    }

    /**
    * @Description: 解析token
    * @Param:
    * @return:
    */
    @RequestMapping("/getUserToToken")
    public RespondDTO getUserToToken(String token) {
        System.err.println(token);
        Claims claims = JwtUtils.parseToken(token);
        return RespondDTO.success(claims);
    }

    /**
    * @Description: 图片验证码
    * @Param:
    * @return:
    */
    @RequestMapping("getValidate")
    public RespondDTO getValidate(String userUnique) {
        if (userUnique != null) {
            redisTemplate.delete(userUnique);
            System.out.println(userUnique + "验证码删除成功");
        }

        ValidateCodeUtil validateCodeUtil = new ValidateCodeUtil();

        String randomCodeImage = validateCodeUtil.getRandomCodeImage();

        String code = validateCodeUtil.getCode();

        String unique = UUID.randomUUID().toString().replace("-", "");

        redisTemplate.opsForValue().set(unique, code, 5, TimeUnit.MINUTES);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("code", randomCodeImage);
        hashMap.put("unique", unique);

        return RespondDTO.success(hashMap);
    }
    
    /**
    * @Description: 判断数据库中的用户名
    * @Param: 
    * @return:  
    */
    @RequestMapping("/isExitAccount")
    public RespondDTO isExit(UserVO userVO){
        RespondDTO judgeAccount = userFrontService.judgeAccount(userVO);
        return judgeAccount;
    }

    /**
    * @Description: 判断数据库中的手机号
    * @Param:
    * @return:
    */
    @RequestMapping("/isExitPhone")
    public RespondDTO isExitPhone(UserVO userVO) {
        RespondDTO exitPhone = userFrontService.isExitPhone(userVO);
        return exitPhone;
    }

    /**
    * @Description: 短信验证码-用户注册
    * @Param:
    * @return:
    */
    @PostMapping("/send")
    public RespondDTO getCodeTel(@RequestBody UserVO userVO) {
        RespondDTO sendCode = userFrontService.sendCode(userVO);
        return sendCode;
    }

    /**
    * @Description: 用户注册
    * @Param:
    * @return:
    */
    @RequestMapping("/register")
    public RespondDTO register(UserVO userVO) {
        RespondDTO register = userFrontService.register(userVO);
        return register;
    }

    /**
    * @Description: 用户登录-手机验证码
    * @Param:
    * @return:
    */
    @PostMapping("/sendLogin")
    public RespondDTO getCodeTelLogin(@RequestBody UserVO userVO) {
        RespondDTO codeTelLogin = userFrontService.getCodeTelLogin(userVO);
        return codeTelLogin;
    }

    /**
    * @Description: 用户登录-手机号是否存在
    * @Param:
    * @return:
    */
    @RequestMapping("/telIsExit")
    public RespondDTO telIsExit(UserVO userVO) {
        RespondDTO telIsExit = userFrontService.telIsExit(userVO);
        return telIsExit;
    }

    /**
    * @Description: 用户登录-手机验证码登录
    * @Param:
    * @return:
    */
    @RequestMapping("/telsLogin")
    public RespondDTO telsLogin(UserVO userVO) {
        RespondDTO telLogin = userFrontService.telLogin(userVO);
        return telLogin;
    }

    /**
    * @Description: 推荐医生
    * @Param:
    * @return:
    */
    @RequestMapping("/commendDoctor")
    public RespondDTO commendDoctor() {
        RespondDTO commendDoctor = userFrontService.commendDoctor();
        return commendDoctor;
    }

}
